<?php
$cs = Yii::app()->clientScript;

$form_name = 'ObjectiveForm[objectives]';
?>

<div class="yiiForm" id="objectives">
<ul id="a_objectives">
<?php

foreach ($objectiveForm->objectives as $topic) {
	echo "<li id='a_node_" . $topic->id . "'>" . $topic->topic->label . "</li>";
	$this->widget('adviser.widgets.CommentLayer', array (
		'topic_id' => $topic->id,
		'columns' => 60,
		'rows' => 5,
		'prefix' => 'a_',
		'adviser_validation' => $topic->adviser_validation,
		'adviser_topic_validation' => $topic->adviser_validation_topic,
		'adviser_has_error' => count($objectiveForm->getErrors('adviser_validation_' . $topic->id)) > 0,
		'owner_validation' => $topic->owner_validation,
		'owner_topic_validation' => $topic->owner_validation_topic,
		'owner_has_error' => count($objectiveForm->getErrors('owner_validation_' . $topic->id)) > 0,
		'comment' => $topic->validation_comments,
		'form_name' => $form_name,
		'read_only' => true
	));
}
?>
</ul>
</div><!-- yiiForm -->

<?php

$script =<<<JS
$("div[id^='a_comment_layer_']").hide();

$("li[id^='a_node_']").each(function(index){
	var keyId = this.id.replace('a_node_', '');
	if ($("#a_adviser_"+keyId).is(":checked") || $("#a_owner_"+keyId).is(":checked"))
	{
		t = $(this);
		t.html("<a id='a_link_comment_layer_"+keyId+"'>"+t.text()+"</a>")
		 .prepend('<img id="a_img_comment_'+keyId+'" src="images/arrow_down_blue.png" />')
		 .click(function(event){
			keyId = this.id.replace('a_node_', '');
			if (
				event.target.id == "a_node_"+keyId ||
				event.target.id == "a_img_comment_"+keyId ||
				event.target.id == "a_link_comment_layer_"+keyId
			){
				$("#a_img_comment_"+keyId).click();
			}
		 });
		$("#a_img_comment_"+keyId).toggle(
			function(){
				//keyId = this.id.replace('a_img_comment_', '');
				this.src = 'images/arrow_up_blue.png';
				$("#a_comment_layer_"+keyId).show();
			}, 
			function(){
				//keyId = this.id.replace('a_img_comment_', '');
				this.src = 'images/arrow_down_blue.png';
				$("#a_comment_layer_"+keyId).hide();
			}
		);
	}
});
JS;
$cs->registerScript('a_topics_form_save', $script);
?>
